<?php
// Show a sliding menu that contains submenus of the top 10 categories.
// This function gets events, gender, type, length from ls_events table
// and creates a slide menu. If there are no events in the table then
// this menu will be empty.
function showTop10Sidebar() {
	// Get events by ageGroup and gender. These are used to lable the side menu headings
	// such as "10 and Under Male", "18-24 Year old Female".
	$query = "SELECT `ageGroup`, `gender` FROM `ls_events` GROUP BY `gender`, `ageGroup`";
	$result = mysql_query($query) or die("Error selecting age groups and gender: " . mysql_error());
	$groups = array();
	while($groupRows = mysql_fetch_assoc($result)) {
		// Using the ageGroup and gender categories above, get the corresponding events that thos categories 
		// have in the database such as frontstroke, backstroke, etc. These will be stored in an associative array
		// which looks like arra("10 and Under Male" => array("frontstroke 50m, backstroke 100m")) and will be
		// displayed as a sliding menu with the key "10 and Under Male" as heading and the inner array as subitems.
		$query2 = "SELECT `eventType`, `eventLength` FROM `ls_events` 
				WHERE `ageGroup` = '" . $groupRows["ageGroup"] . "'
				AND `gender` = '" . $groupRows["gender"] . "'
				GROUP BY `eventType`
				ORDER BY `eventType` ASC, `eventLength` ASC";
		$result2 = mysql_query($query2) or die("Error selecting event types and length: " . mysql_error());
		$types = array();
		while($typeRows = mysql_fetch_array($result2)) {
			array_push($types, $typeRows["eventType"] . " " . $typeRows["eventLength"]);
		}
		
		$groups[($groupRows["ageGroup"] . ($string = ($groupRows["ageGroup"] != "10 and Under") ? " Year old" : "") . " " . $groupRows["gender"])] = $types;
	}
	// show the side menu if there are records in the ls_events table
	if(!is_null($groups)) {
		echo "<div class='sidebar-box'><div id='slideMenu'>";
		showMenus($groups);
		echo "</div></div>";
	}
}

// Show the individual submenus, these menus will slide when clicked on by using jquery.
function showMenus($groups, $factor = 2) {
	$showPointer = "style='pointer:cursor;'";
	$noDisplay = "style='display:none;'";
	foreach($groups as $k => $v) {
		// Get position of 'Male', 'Female', or 'Year old' in the string so that
		// we can get a substring without those 3 words for use in our menu links bellow.
		$pos1 = false;
		if(strpos($k, "Female")) {
			$pos1 = strpos($k, "Female") - 1;
			$gender = "Female";
		}
		else if($pos1 == false && $pos1 = strpos($k, "Male")) {
			$pos1 = strpos($k, "Male") - 1;
			$gender="Male";
		}
		else if($pos1 == false) {
			$pos1 = strpos($k, "Mixed") - 1;
			$gender="Mixed";
		}
		if(strpos($k, "Year old")) {
			$pos1 = strpos($k, "Year old") - 1;
		}
		
		// Display the menu headings
		echo "<h3 class='menuHead' $showPointer>$k</h3>
			<ul class='menuItems' $noDisplay>";
		// Display the list of event types under each menu heading.
		// These will be hidden until the user clicks the heading.
		foreach($v as $type) {
			$pos2 = false;
			// Get position of '4 x' in the string so that we can get a substring without 
			// those that word for use in our menu links bellow.
			if(strpos($type, "4 x ")) {
				$pos2 = strpos($type, "4 x ") - 1;
				$typeVal = substr($type, 0, $pos2);
			}
			else {
				$typeVal = ereg_replace("[0-9]", "", $type);
				$pos2 = strlen($typeVal) - 1;
				$typeVal = substr($typeVal, 0, $pos2);
			}
			
			// Remove spaces and alphabets to het the length. The length
			// canonly contain numbers, x, m, or spaces.
			$len = ereg_replace("[^0-9|^x|^m|^ ]", "", $type);
			if(substr($len, 0, 2) == "  ") {
				$len = substr($len, 2);
			}
			else if(substr($len, 0, 1) == " ") {
				$len = substr($len, 1);
			}
			
			// Print the list items and also generate the links for each menu item.
			echo "<li class='page_item'><a href=\"javascript:getTop10('" . substr($k, 0, $pos1) . "', '" . $gender . "', '" . ereg_replace("[x]", "", $typeVal) . "', '" . $len . "');window.scrollTo(0,200);\">$type</a></li>";
		}
		echo "</ul>";
	}
}
?>